{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文本生成\n",
    "\n",
    "## 说明\n",
    "本文展示了如何使用appbuilder内置组件以及如何快速构建全新的文本生成组件。以下是一个在营销场景下生成各种文本内容的样例。\n",
    "\n",
    "## 概览\n",
    "具体地，本样例按照以下流程进行文本生成：\n",
    "1. 使用**空模板**构建一个全新的文本生成组件：商品信息生成组件。\n",
    "2. 输入一个商品，使用新构建的商品信息生成组件生成该商品的商品信息。\n",
    "3. 基于生成的商品信息，使用内置的**问答对挖掘**组件生成问答对。生成的问答对可用于客服等。\n",
    "4. 基于生成的商品信息，使用内置的**风格写作**组件为该商品生成一份【小红书】文案。\n",
    "5. 使用内置的**标签抽取**组件对生成的文案进行标签抽取。抽取出的标签可用于检索文案。\n",
    "\n",
    "以下我们会演示如何实现我们自己的定制化能力。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 流程演示\n",
    "### Step 0: 安装Python SDK\n",
    "我们的appbuilder支持使用pip安装（要求Python >= 3.8）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "pip install appbuilder-sdk"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码中需要配置用户的`APPBUILDER_TOKEN`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# 设置环境变量\n",
    "os.environ['APPBUILDER_TOKEN'] = '...'"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "安装成功后，我们就可以搭建我们自己的能力了。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1: 商品信息生成\n",
    "基于appbuilder提供的**空模板**，用户可以自定义文本生成组件以解决多样化的需求。这里我们构建一个**商品信息生成**组件以生成商品信息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from appbuilder import Message, Playground\n",
    "\n",
    "# 输入到大模型中的prompt的模板\n",
    "prompt_template = \\\n",
    "'''输入商品名，我需要你为我生成该商品的商品信息。\n",
    "\n",
    "要求：\n",
    "- 你生成的商品信息应该包含多方面信息。\n",
    "- 开头和结尾不需要有其他与商品信息无关的内容。\n",
    "\n",
    "商品名：{product_name}\n",
    "商品信息：\n",
    "'''\n",
    "# 创建商品信息生成组件\n",
    "product_information_generation = Playground(prompt_template=prompt_template, model='Qianfan-Agent-Speed-8K')\n",
    "\n",
    "# 获取商品信息\n",
    "# 填充prompt_template参数的参数映射表，需要与prompt_template对应\n",
    "prompt_template_kwargs = {\n",
    "    'product_name': '特斯拉Model Y'\n",
    "}\n",
    "response = product_information_generation(Message(prompt_template_kwargs), stream=False, temperature=0.5)\n",
    "product_information = response.content\n",
    "print(f'商品信息：\\n{product_information}')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出结果：\n",
    "\n",
    "```\n",
    "商品信息：\n",
    "特斯拉Model Y是一款电动汽车，它拥有许多独特的功能和特性。以下是它的主要特点和优势：\n",
    "\n",
    "特斯拉Model Y是一款电动汽车，它采用纯电力驱动，没有排放和噪音污染，具有零启动加速、零延迟、零维护的特性。\n",
    "特斯拉Model Y拥有先进的自动驾驶技术，可以自动识别交通状况并做出相应的反应，大大提高了驾驶安全性。\n",
    "特斯拉Model Y的电池寿命长，充电效率高，可以在短时间内将电池充满，同时支持快速充电和慢充两种方式。\n",
    "特斯拉Model Y的外观设计独特，线条流畅，充满未来感，内部空间宽敞，乘坐舒适。\n",
    "特斯拉Model Y的价格相对较高，但性价比高，是一款非常值得购买的汽车。\n",
    "\n",
    "总之，特斯拉Model Y是一款集未来科技、安全性、舒适性和实用性于一体的电动汽车。\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2: 问答对生成\n",
    "基于Step 1生成的商品信息我们生成一些问答对，这里我们使用内置的**问答对生成**组件。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from appbuilder import QAPairMining\n",
    "\n",
    "# 初始化问答对生成组件\n",
    "qa_pair_mining = QAPairMining(model='Qianfan-Agent-Speed-8K')\n",
    "\n",
    "# 获取问答对\n",
    "response = qa_pair_mining(Message(product_information), stream=False, temperature=1e-10)\n",
    "qa_pairs = response.content\n",
    "print(f'问答对：\\n{qa_pairs}')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出结果：\n",
    "\n",
    "```\n",
    "问答对：\n",
    "问题：特斯拉Model Y有什么特点？\n",
    "答案：特斯拉Model Y是一款电动汽车，它采用纯电力驱动，没有排放和噪音污染，具有零启动加速、零延迟、零维护的特性。\n",
    "\n",
    "问题：特斯拉Model Y有什么优势？\n",
    "答案：特斯拉Model Y拥有先进的自动驾驶技术，可以自动识别交通状况并做出相应的反应，大大提高了驾驶安全性。\n",
    "\n",
    "问题：特斯拉Model Y的电池寿命如何？\n",
    "答案：特斯拉Model Y的电池寿命长，充电效率高，可以在短时间内将电池充满，同时支持快速充电和慢充两种方式。\n",
    "\n",
    "问题：特斯拉Model Y的外观设计如何？\n",
    "答案：特斯拉Model Y的外观设计独特，线条流畅，充满未来感，内部空间宽敞，乘坐舒适。\n",
    "\n",
    "问题：特斯拉Model Y的价格是多少？\n",
    "答案：特斯拉Model Y的价格相对较高，但性价比高，是一款非常值得购买的汽车。\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3: 营销文案生成\n",
    "基于Step 1生成的商品信息生成【小红书】文案，这里我们使用内置的**风格写作**组件。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from appbuilder import StyleWriting\n",
    "\n",
    "# 构造query\n",
    "query = \\\n",
    "f'''请你基于以下商品信息生成文案：\n",
    "{product_information}\n",
    "'''\n",
    "\n",
    "# 初始化风格写作组件\n",
    "style_writing = StyleWriting(model='Qianfan-Agent-Speed-8K')\n",
    "\n",
    "# 获取小红书文案\n",
    "response = style_writing(Message(query), style_query='小红书', length=300)\n",
    "copywriting = response.content\n",
    "print(f'文案：\\n{copywriting}')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出结果：\n",
    "\n",
    "```\n",
    "【特斯拉Model Y，一款值得拥有的未来科技之车！🚀】\n",
    "\n",
    "🚗Model Y作为一款电动汽车，具有许多独特的功能和特性。它采用纯电力驱动，没有排放和噪音污染，具有零启动加速、零延迟、零维护的特性。在城市道路上行驶，它就像是一阵清风，让你轻松应对各种交通状况。💨\n",
    "\n",
    "🚗特斯拉Model Y还拥有先进的自动驾驶技术，可以自动识别交通状况并做出相应的反应，大大提高了驾驶安全性。在行驶过程中，它就像是一位智能助手，为你提供安全、舒适的驾驶体验。🛡️\n",
    "\n",
    "🚗Model Y的电池寿命长，充电效率高，可以在短时间内将电池充满，同时支持快速充电和慢充两种方式。无论是长途旅行还是城市代步，它都能满足你的需求。⚡\n",
    "\n",
    "🚗Model Y的外观设计独特，线条流畅，充满未来感，内部空间宽敞，乘坐舒适。无论是驾驶还是乘坐，都能感受到它的豪华与舒适。🌟\n",
    "\n",
    "💰虽然价格相对较高，但性价比高，是一款非常值得购买的汽车。它不仅是一款车，更是一种生活方式的体现。🚗\n",
    "\n",
    "#未来科技 #特斯拉 #安全性 #舒适性 #实用性 #电动汽车\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4: 标签抽取\n",
    "基于Step 3生成的文案进行标签抽取，这里我们使用内置的**标签抽取**组件。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from appbuilder import TagExtraction\n",
    "\n",
    "# 初始化标签抽取组件\n",
    "tag_extraction = TagExtraction(model='Qianfan-Agent-Speed-8K')\n",
    "\n",
    "# 获取标签\n",
    "response = tag_extraction(Message(copywriting), stream=False, temperature=1e-10)\n",
    "tags = response.content\n",
    "print(f'标签：\\n{tags}')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出结果：\n",
    "\n",
    "```\n",
    "标签：\n",
    "1.特斯拉\n",
    "2.Model Y\n",
    "3.未来科技\n",
    "4.纯电力驱动\n",
    "5.自动驾驶\n",
    "6.电池寿命\n",
    "7.充电效率\n",
    "8.外观设计\n",
    "9.内部空间\n",
    "10.性价比\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "bf58028eab835c8b5fcb0ca40b311fc21e6cba5ccf47f1b42d31ca2545cde380"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
